package com.hfzy.ihk.web.analyse.conf;

import com.hfzy.ihk.common.support.redis.RedisConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * Created by Administrator on 2018/1/29.
 *
 * @author wws
 */
@Configuration
@Import(RedisConfig.class)
public class ElasticSearchConfig {

    @Value("${spring.profiles.active}")
    public String  profile;

    public String indexName() {
        return "hf_analyse_"+profile;
    }

//    public String indexName() {
//        return "hfcrm_local";
//    }

    /*private static final Logger logger = LoggerFactory.getLogger(ElasticsearchAutoConfiguration.class);

    private final ElasticsearchProperties settingProperties;

    private TransportClient client;

    public ElasticSearchConfig(ElasticsearchProperties settingProperties) {
        this.settingProperties = settingProperties;
    }

    @Bean
    public Client elasticsearchClient()  {
        try {
            Assert.hasText(settingProperties.getClusterNodes(), "[Assertion failed] clusterNodes settings missing.");

            TransportClient preBuiltTransportClient = new PreBuiltTransportClient(settings());

            for (String clusterNode:settingProperties.getClusterNodes().split(",")) {

                logger.info("adding transport node : " + clusterNode);

                String InetSocket [] = clusterNode.split(":");
                String  Address = InetSocket[0];
                Integer  port = Integer.valueOf(InetSocket[1]);
                preBuiltTransportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(Address),port ));
            }

            preBuiltTransportClient.connectedNodes();

            client = preBuiltTransportClient;

        } catch (UnknownHostException e) {
            logger.error(e.getMessage());
        }
        return client;
    }

    *//**
     * 初始化默认的client
     *//*
    private Settings settings(){
        Settings settings = Settings.builder()
                .put("cluster.name",settingProperties.getClusterName())
                .put("client.transport.sniff",true)
                .build();
        return settings;
    }

    @Bean
    public Client createTransportClient() throws Exception {
        TransportClientFactoryBean factory = new TransportClientFactoryBean();
        factory.setClusterName(this.settingProperties.getClusterName());
        factory.setClusterNodes(this.settingProperties.getClusterNodes());
        factory.setProperties(createProperties());
        factory.afterPropertiesSet();
        TransportClient client = factory.getObject();
        this.client = client;
        return client;
    }

    private Properties createProperties() {
        //检测是否有其他client参数
        if(this.settingProperties.getProperties().isEmpty())
            return null;

        Properties properties = new Properties();
        properties.put("cluster.name", this.settingProperties.getClusterName());
        properties.putAll(this.settingProperties.getProperties());
        return properties;
    }

    @PreDestroy
    public void destroy() throws Exception {
        if (this.client != null) {
            try {
                if (logger.isInfoEnabled()) {
                    logger.info("Closing Elasticsearch client");
                }
                try {
                    this.client.close();
                }
                catch (NoSuchMethodError ex) {
                    // Earlier versions of Elasticsearch had a different method name
                    ReflectionUtils.invokeMethod(
                            ReflectionUtils.findMethod(Releasable.class, "release"),
                            this.client);
                }
            }
            catch (final Exception ex) {
                if (logger.isErrorEnabled()) {
                    logger.error("Error closing Elasticsearch client: ", ex);
                }
            }
        }
    }*/

}
